package com.cory.module.job.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cory.boot.base.BaseController;
import com.cory.boot.domain.bean.Result;
import com.cory.boot.utils.SysUtils;
import com.cory.module.job.core.JobFactory;
import com.cory.module.job.domain.entity.JobExcelImport;
import com.cory.module.job.service.JobExcelImportDetailService;
import com.cory.module.job.service.JobExcelImportService;
import com.github.pagehelper.PageInfo;

/**
 * @author xiongcong
 * @date 2023-05-09
 */
@Controller
@RequestMapping("admin/job/excel/import")
public class JobExcelImportController extends BaseController {
    @Autowired
    private JobExcelImportService jobExcelImportService;
    @Autowired
    private JobExcelImportDetailService jobExcelImportDetailService;

    @RequestMapping("list")
    public String list(JobExcelImport param, Model model) {
        param.setTenantId(SysUtils.getLoginTenantId());
        param.setSystem(SysUtils.getSystem());

        super.startPage();
        List<JobExcelImport> list = jobExcelImportService.list(param);

        model.addAttribute("page", new PageInfo<>(list));
        model.addAttribute("param", param);
        model.addAttribute("annotationMap", JobFactory.getExcelImportJobAnnotationMap());
        return "admin/job/excel/import/list";
    }

    @GetMapping("error")
    public String error(Integer id, ModelMap model) {
        model.addAttribute("list", jobExcelImportDetailService.errorList(id));
        return "admin/job/excel/import/error";
    }

    @RequestMapping("error/export")
    public void error_export(Integer id, HttpServletResponse response) throws Exception {
        JobExcelImport job = jobExcelImportService.getById(id);
        JobFactory.getExcelImportJobHandle(job.getType()).exportError(request, response, id);
    }

    @RequestMapping("form")
    public String form(JobExcelImport info, ModelMap model) {
        assertNotNull(info.getType());
        model.addAttribute("info", info);
        model.addAttribute("annotation", JobFactory.getExcelImportJobAnnotation(info.getType()));
        return "admin/job/excel/import/form";
    }

    @RequestMapping("save")
    @ResponseBody
    public Result<?> save(JobExcelImport info) {
        info.setTenantId(SysUtils.getLoginTenantId());
        info.setSystem(SysUtils.getSystem());

        jobExcelImportService.save(info);
        return Result.success("保存成功");
    }

    @RequestMapping("tpl")
    public void tpl(String type, HttpServletResponse response) throws Exception {
        JobFactory.getExcelImportJobHandle(type).tpl(request, response);
    }
}
